export const baseUrl = "http://api.tdcode.xyz/"
export const request = (options) => {
	const token = uni.getStorageSync('token')
	if (token) {
		options.header = options.header || {};
		options.header['token'] = token;
	}
	const Url = baseUrl + "/api" + options.url
	return new Promise((resolve, reject) => {
		if (options.isUpload) {
			return uni.uploadFile({
				url: Url,
				filePath: options.data.url,
				header: options.header || {},
				method: options.method || "GET",
				data: options.data,
				name: "file",
				success: (res) => {
					setTimeout(() => {
						resolve(res.data);
					}, 1000);
				},
			});
		} else {

			uni.request({
				url: Url,
				header: options.header || {},
				method: options.method || "GET",
				data: options.data,
				success: (res) => {
					if (res.data.code == 200) {
						resolve(res.data);
					} else if (res.data.code == 210) {
						uni.showModal({
							content: res.data.msg || '当前操作需要登录哦!',
							title: '温馨提示',
							success: (result) => {
								const currentRoute = getCurrentPages()[0]['route']
								const tabRoute = ['pages/mine/mine',
									'pages/message/message'
								]
								if (result.confirm) {
									uni.redirectTo({
										url: "/pages/login/login"
									})
								} else {
									if (tabRoute.includes(currentRoute)) {
										uni.reLaunch({
											url: '/pages/index/index'
										})
										return
									}
									uni.navigateBack({
										delta: 1
									})
									resolve(res.data);
								}
							}

						})
					} else {
						if (res.data && res.data.code || res.data.code == 0) {
							uni.showModal({
								title: '错误提示',
								content: res.data.msg || '未知错误',
								showCancel: false
							});
							reject(res.data);
						}
					}
				},
				fail: (err) => {
					uni.showModal({
						title: '请求失败',
						content: err.errMsg || '网络连接错误'
					});
					reject(err);
				},
				complete: res => {
					uni.hideLoading()
				}
			})

		}
	})
}